package com.china08.yxyapi.api.manage.tec.teaching.homework.workassign.question;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.es.ElasticYxyYxyQuesResource;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.security.profile.YxyProfile;
import com.china08.yxyapi.service.manage.tec.teaching.homework.HomeworkQuestionService;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;

import io.swagger.annotations.ApiOperation;

/**
 * 收藏试题
 * 
 * @author renruihu
 *
 */
@RestController
@CrossOrigin
public class HomeworkQuestionCollectionSchController extends YxyApiBaseController {

    @Autowired
    private HomeworkQuestionService homeworkQuestionService;

    @ApiOperation(value = "作业系统我的收藏查询")
    @ResponseBody
    @RequestMapping(value = "/api_yw/manage/tec/teaching/homework/workassign/question/collection", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Page<RespModel> precess(HttpServletRequest request, HttpServletResponse response,
            @RequestBody ReqModel reqModel) throws JsonParseException, JsonMappingException, IOException {
        YxyProfile profile = getProfile(request, response);
        String userId = profile.getId();
        List<RespModel> list = new ArrayList<HomeworkQuestionCollectionSchController.RespModel>();
        Pageable pageable = new PageRequest(reqModel.page, reqModel.size);
        Page<ElasticYxyYxyQuesResource> page = homeworkQuestionService.collectionfindByMy(reqModel.phase,
                reqModel.subject, userId, reqModel.difficultyLevel, reqModel.questionType, pageable);
        for (ElasticYxyYxyQuesResource elasticYxyYxyQuesResource : page) {
            RespModel model = new RespModel();
            model.difficultyLevel = elasticYxyYxyQuesResource.getDifficultyLevel();
            model.id = elasticYxyYxyQuesResource.getId();
            model.knowledgeTexts = elasticYxyYxyQuesResource.getKnowledgeTexts();
            model.quesBody = elasticYxyYxyQuesResource.getQuesBody();
            model.questionType = elasticYxyYxyQuesResource.getQuestionType();
            model.quesTimes = homeworkQuestionService.countQuestionUse(model.id);
            model.knowIds = elasticYxyYxyQuesResource.getKnowledgeIds();
            if (model.quesBody == null) {
                continue;
            }
            list.add(model);
        }
        Page<RespModel> resPage = new PageImpl<HomeworkQuestionCollectionSchController.RespModel>(list, pageable,
                page.getTotalElements());
        return resPage;
    }

    protected static class ReqModel extends YxyApiPageReqModel {
        public String phase;
        public String subject;
        public String difficultyLevel;
        public String questionType;
        public String isUsed;

        @Override
        public void autoCheck() {
            Assert.notNull(StringUtils.defaultIfBlank(phase, null), "请填写学段");
            Assert.notNull(StringUtils.defaultIfBlank(subject, null), "请填写学科");
        }

    }

    protected static class RespModel {
        public String id;
        public String difficultyLevel;
        public List<String> knowledgeTexts;
        public String questionType;
        public Object quesBody;
        public Long quesTimes;
        public List<Integer> knowIds;
    }

}
